package org.brain.bean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import org.brain.pojos.*;
import org.brain.tool.ToolBean;

import org.brain.dbo.DBOperate;

/**
 * @author LiuFanke
 *
 */
@ManagedBean(name = "selectBean")
@SessionScoped
public class SelectBean {
    private String currentType = null;
    private String currentType1 = null;
    private List<SelectItem> firstList = new ArrayList<SelectItem>();
    private List<SelectItem> secondList = new ArrayList<SelectItem>();
    private int firstSize = 0;
    private int secondSize = 0;
    private User user;
    public void initItems() {
    		firstList.clear();
    		secondList.clear();
    		user = ToolBean.getUserSession();
    		List<Treemenu> items = DBOperate.getTreeMenu(user.getUsername(), 0);
    		if(items.size()!=0){
    			firstSize = 1;
    		for(Treemenu menu:items)
    		{
    				firstList.add(new SelectItem(menu.getNodeid(),menu.getNodename()));
    		}
    		currentType = items.get(0).getNodeid().toString();
    		
    		List<Treemenu> items1 = DBOperate.getTreeMenu(user.getUsername(), Integer.parseInt(currentType));
    			secondSize = items1.size();
    		for(Treemenu menu:items1)
    		{
    			    secondList.add(new SelectItem(menu.getNodeid(),menu.getNodename()));
    		}
    		currentType1 = items1.get(0).getNodeid().toString(); 
    		}else
    		{
    			firstSize = 0;
    		}
    		
    }
    
    public void valueChanged(ValueChangeEvent event) {
    	
    	currentType = (String)event.getNewValue();
    	secondList.clear();
    	
    	List<Treemenu> items1 = DBOperate.getTreeMenu(user.getUsername(), Integer.parseInt(currentType));
		for(Treemenu menu:items1)
		{
			    secondList.add(new SelectItem(menu.getNodeid(),menu.getNodename()));
		}
    	
        
    }

    public List<SelectItem> getFirstList() {
    	
        return firstList;
    }

    public List<SelectItem> getSecondList() {
        return secondList;
    }

    

    public String getCurrentType() {
        return currentType;
    }

    public void setCurrentType(String currentType) {
        this.currentType = currentType;
    }

	public String getCurrentType1() {
		return currentType1;
	}

	public void setCurrentType1(String currentType1) {
		this.currentType1 = currentType1;
	}

	public void setFirstList(List<SelectItem> firstList) {
		this.firstList = firstList;
	}

	public void setSecondList(List<SelectItem> secondList) {
		this.secondList = secondList;
	}

	public int getFirstSize() {
		initItems();
		return firstSize;
	}

	public void setFirstSize(int firstSize) {
		this.firstSize = firstSize;
	}

	public int getSecondSize() {
		return secondSize;
	}

	public void setSecondSize(int secondSize) {
		this.secondSize = secondSize;
	}
	

    
}
